#include "stdafx.h"
#include "Menus.h"

#include "Engine.h"
#include "States.h"

const Color font_invisible(0, 0, 0, 0);
const Color font_color(0, 0, 0, 255);
const Color font_color_selected(128, 128, 128);

const Vector2 continue_pos(10.0f, 10.0f);
const Vector2 new_game_pos(10.0f, 34.0f);
const Vector2 highscores_pos(10.0f, 58.0f);
const Vector2 options_pos(10.0f, 82.0f);
const Vector2 quit_pos(10.0f, 106.0f);

const Vector2 sound_vol_pos(140.0f, 10.0f);
const Vector2 sound_slider_pos(140.0f, 34.0f);
const Vector2 music_vol_pos(140.0f, 40.0f);
const Vector2 music_slider_pos(140.0f, 64.0f);
const Vector2 mouse_speed_pos(140.0f, 80.0f);
const Vector2 mouse_slider_pos(140.0f, 104.0f);
const Vector2 resolution_pos(140.0f, 120.0f);
const Vector2 resolution_selector_pos(140.0f, 142.0f);
const Vector2 fullscreen_pos(140.0f, 168.0f);
const Vector2 vsync_pos(140.0f, 192.0f);
const Vector2 back_pos(140.0f, 216.0f);

void MainMenu::render(const StateManager& state_manager)
{
	gui->doButton(continue_pos, "CONTINUE");
	if(gui->doButton(new_game_pos, "NEW GAME"))
		state_manager.pushState(GAME);

	gui->doButton(highscores_pos, "HIGHSCORES");

	if(gui->doButton(options_pos, "OPTIONS"))
		state_manager.pushState(OPTIONS);
	
	if(gui->doButton(quit_pos, "QUIT"))
		state_manager.popState();
}

void MainMenu::fade(const StateManager& state_manager, float amount, uint from, uint to)
{
	if(to == HIGHSCORES || to == OPTIONS || from == HIGHSCORES || from == OPTIONS)
	{
		Color color = Color::lerp(font_color_selected, font_color, amount);
		font->draw(continue_pos, "CONTINUE", color);
		font->draw(new_game_pos, "NEW GAME", color);
		font->draw(highscores_pos, "HIGHSCORES", color);
		font->draw(options_pos, "OPTIONS", color);
		font->draw(quit_pos, "QUIT", color);
	}
	if(to == GAME || from == GAME)
	{
		float position = (10.0f - 100.0f) + (amount*amount * 100.0f);
		Color color = Color::lerp(font_invisible, font_color, amount);

		font->draw(Vector2(position, continue_pos.y), "CONTINUE", color);
		font->draw(Vector2(position, new_game_pos.y), "NEW GAME", color);
		font->draw(Vector2(position, highscores_pos.y), "HIGHSCORES", color);
		font->draw(Vector2(position, options_pos.y), "OPTIONS", color);
		font->draw(Vector2(position, quit_pos.y), "QUIT", color);
	}
}

OptionsMenu::OptionsMenu(RendererPtr main_renderer, BitmapFontPtr font, CollapseGUIStylePtr gui_style)
	: State(main_renderer), font(font), gui_style(gui_style)
{
	Engine e;
	gui = e.getGUI();

	DisplayModesPtr modes = video_device->getModes();
	for(uint n = 0; n < modes->getModeCount(); n++)
	{
		resolutions.push_back(modes->getMode(n).getDescription());
	}

	VideoSettings& settings = video_device->getSettings();
	
	uint current_mode = modes->getCurrentAppMode(video_device->getSettings());
	gui->setSelectorState(Vector2(140.0f, 142.0f), current_mode);
	gui->setSwitchState(Vector2(140.0f, 168.0f), settings["Fullscreen"]);
	gui->setSwitchState(Vector2(140.0f, 192.0f), settings["VSync"]);

	// TODO: set slider states to proper values
	gui->setSliderState(Vector2(140.0f, 34.0f), 0.5f);
	gui->setSliderState(Vector2(140.0f, 64.0f), 0.5f);
	gui->setSliderState(Vector2(140.0f, 104.0f), 0.5f);
}

void OptionsMenu::render(const StateManager& state_manager)
{
	MainMenuPtr main_menu = boost::static_pointer_cast<MainMenu>(state_manager.getState(MAIN_MENU));
	main_menu->fade(state_manager, 0.0f, OPTIONS, -1);

	gui->doLabel(sound_vol_pos, "SOUND VOLUME:");
	gui->doSlider(sound_slider_pos);
	gui->doLabel(music_vol_pos, "MUSIC VOLUME:");
	gui->doSlider(music_slider_pos);
	gui->doLabel(mouse_speed_pos, "MOUSE SPEED:");
	gui->doSlider(mouse_slider_pos);
	gui->doLabel(resolution_pos, "RESOLUTION:");
	uint mode = gui->doSelector(resolution_selector_pos, resolutions);
	bool fullscreen = gui->doSwitch(fullscreen_pos, "FULLSCREEN");
	bool vsync = gui->doSwitch(vsync_pos, "VSYNC");
	
	if(gui->doButton(back_pos, "BACK"))
	{
		// Set new video params
		VideoSettings& settings = video_device->getSettings();
		video_device->getModes()->setMode(settings, mode);
		settings["Fullscreen"] = fullscreen;
		settings["VSync"] = vsync;
		video_device->switchMode();

		// Return to previuos state
		state_manager.popState();
	}
}

void OptionsMenu::fade(const StateManager& state_manager, float amount, uint from, uint to)
{
	if(to == MAIN_MENU || from == MAIN_MENU)
	{
		float position = 10.0f + (amount*amount * 130.0f);

		Color color = Color::lerp(font_invisible, font_color, amount);
		
		font->draw(Vector2(position, sound_vol_pos.y), "SOUND VOLUME:", color);
		gui_style->drawSimpleSlider(Vector2(position, sound_slider_pos.y), gui->getSliderState(sound_slider_pos), color);
		font->draw(Vector2(position, music_vol_pos.y), "MUSIC VOLUME:", color);
		gui_style->drawSimpleSlider(Vector2(position, music_slider_pos.y), gui->getSliderState(music_slider_pos), color);
		font->draw(Vector2(position, mouse_speed_pos.y), "MOUSE SPEED:", color);
		gui_style->drawSimpleSlider(Vector2(position, mouse_slider_pos.y), gui->getSliderState(mouse_slider_pos), color);
		font->draw(Vector2(position, resolution_pos.y), "RESOLUTION:", color);

		font->draw(Vector2(position, resolution_selector_pos.y), 
			"[" + resolutions[gui->getSelectorState(resolution_selector_pos)] + "]", color);
		
		font->draw(Vector2(position, fullscreen_pos.y), 
			gui->getSwitchState(fullscreen_pos) ? "+ FULLSCREEN" : "- FULLSCREEN", color);
		
		font->draw(Vector2(position, vsync_pos.y), gui->getSwitchState(vsync_pos) ? "+ VSYNC" : "- VSYNC", color);
		font->draw(Vector2(position, back_pos.y), "BACK", color);
	}
}